import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/model/group_details_entity.dart';
import 'package:flutter_ncsjqz_getx/page/rong_im/util/rong_widget.dart';
import 'package:get/get.dart';

/// <summary>
/// author：zwb
/// dateTime： 2022/1/19 16:08
/// filePath： lib/page/rong_im/chat_setting/group/group_user_view.dart
/// desc: 群员列表
/// <summary>
///
class GroupUserView extends StatefulWidget {
  final List<GroupDetailsDataUsers>? userList;
  const GroupUserView({Key? key, this.userList: const []}) : super(key: key);

  @override
  _GroupUserViewState createState() => _GroupUserViewState();
}

class _GroupUserViewState extends State<GroupUserView> {
  List<int> selectUserId = [];
  // 搜索结果
  List<GroupDetailsDataUsers>? searchUserList = [];

  searchName(name){
    searchUserList = [];
    widget.userList!.forEach((element) {
      if(element.remarkName!.contains(name)){
        searchUserList!.add(element);
      }
    });
    setState(() { });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: RongWidget.appBarWidget(title: "群员列表 (${widget.userList!.length})",actions: [
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 90,
              height: 35,
              margin: EdgeInsets.only(right: 15),
              child: RongWidget.gradientButton("删除 (${selectUserId.length})",onTap: (){
                if(selectUserId.length > 0){
                  Get.back<List<int>>(result: selectUserId);
                }
              },color:selectUserId.length == 0 ?Colors.grey : Colors.redAccent,),
            ),
          ],
        ),
      ]),
      body: Column(
        children: [
          headSearch(),
          Expanded(
            child: ListView.separated(
              physics: BouncingScrollPhysics(),
              padding: EdgeInsets.all(0),
              separatorBuilder: (BuildContext context, int index) {
                return Divider(color: Colors.grey.shade300,);
              },
              itemCount: searchUserList!.length > 0 ? searchUserList!.length : widget.userList!.length,
              itemBuilder: (BuildContext context, int index) {
                return ListTile(
                  onTap: (){
                    // 群聊、隔离 进行多选操作
                    if(selectUserId.contains(searchUserList!.length > 0 ? searchUserList![index].userId : widget.userList![index].userId)){
                      selectUserId.remove(searchUserList!.length > 0 ? searchUserList![index].userId : widget.userList![index].userId);
                    }else{
                      selectUserId.add(searchUserList!.length > 0 ? searchUserList![index].userId! : widget.userList![index].userId!);
                    }
                    setState(() { });
                  },
                  title: Row(
                    children: [
                      Icon(
                        selectUserId.contains(searchUserList!.length > 0 ? searchUserList![index].userId : widget.userList![index].userId)?Icons.check_circle:Icons.radio_button_unchecked,
                        color: selectUserId.contains(searchUserList!.length > 0 ? searchUserList![index].userId : widget.userList![index].userId)?Colors.green:Colors.grey,
                      ),
                      SizedBox(width: 15,),
                      RongWidget.imgNet("${searchUserList!.length > 0 ? searchUserList![index].picUrl : widget.userList![index].picUrl}",width: 50,height: 50,borderRadius: BorderRadius.circular(5)),
                      RongWidget.text("  ${searchUserList!.length > 0 ? searchUserList![index].remarkName : widget.userList![index].remarkName ?? ''}"),
                    ],
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  /// 搜索栏
  Widget headSearch() {
    return Container(
      padding: EdgeInsets.all(15),
      child: RongWidget.rongInput(
        // controller: logic.textEditingControllerSearch,
        circular: 5,
        height: 45,
        bgColor: Colors.grey.shade100,
        prefixIcon: Icon(Icons.search,color: Colors.grey[400],),
        hintText: "搜索好友",
        hintStyle: TextStyle(color: Colors.grey),
        onChanged: (v){
          searchName(v);
        },
      ),
    );
  }
}
